package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Admin;
import com.demo.villageresidence.pojo.Notice;
import com.demo.villageresidence.service.AdminService;
import com.demo.villageresidence.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Autowired
    private AdminService adminService;

    @PostMapping("addNotice")
    public Result addNotice(@RequestBody Notice notice) {
        notice.setIsDelete(0);
        if (notice.getNoticeDigest().length() >= 200){
            notice.setNoticeDigest(notice.getNoticeDigest().substring(0, 200));
            System.out.println("公告内容过长，已经截取为200个字符");
        }else {
            notice.setNoticeDigest(notice.getNoticeDigest());
        }
        // 获取当前日期
        LocalDateTime localDate = LocalDateTime.now();
        notice.setAnnceTime(localDate);

        noticeService.save(notice);

        return Result.ok(notice.getNoticeId());
    }

    @GetMapping("getAllNotice")
    public Result getAllNotice() {
        List<Notice> noticeList = noticeService.query().orderByDesc("annce_time").list();
        noticeList.forEach(notice -> {
            notice.setAdmin(adminService.getById(notice.getAdminId()));
        });
        return Result.ok(noticeList);
    }

    /**
     * 分页查询
     * @param current 当前页
     * @param pageSize  每页显示条数
     * @return
     */
    @GetMapping("getNoticeByPage")
    public Result getNoticeByPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                  @RequestParam(value = "pageSize", defaultValue = "10")  Integer pageSize) {
        // 根据用户查询
        Page<Notice> page = noticeService.query().orderByDesc("annce_time")
                .page(new Page<>(current, pageSize));
        // 获取当前页数据
        List<Notice> records = page.getRecords();
        // 获取总条数
        long total = page.getTotal();
        return Result.ok(records,total);
    }

    /**
     * 根据id查询
     * @param noticeId
     * @return
     */
    @GetMapping("getNoticeById/{noticeId}")
    public Result getNoticeById(@PathVariable("noticeId") Integer noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return Result.ok(notice);
    }

    @PutMapping("updateNotice")
    public Result updateNotice(@RequestBody Notice notice) {

        if (notice.getNoticeDigest().length() >= 200){
            notice.setNoticeDigest(notice.getNoticeDigest().substring(0, 200));
            System.out.println("公告内容过长，已经截取为200个字符");
        }else {
            notice.setNoticeDigest(notice.getNoticeDigest());
        }
        // 获取当前日期
        LocalDateTime localDate = LocalDateTime.now();
        notice.setAnnceTime(localDate);


        if (noticeService.updateById(notice) != true){
            return Result.fail("修改失败");
        }
        return Result.ok("修改成功");
    }





}
